<template>
  <div>
    <div class="control_filter_wrap" style="background-color: #f8f8f8">
      <van-row>
        <van-col span="6">
          <dropDown :title="'总榜'" :popup-options="topListdict" :popup-title="'请选择榜单'" @onFinish="onFinishType" />
        </van-col>
        <van-col span="6">
          <dropDown ref="categoryDropDown" :title="'项目'" :popup-options="categoryFilterList" :field-names="categoryFieldNames" :popup-title="'请选择项目'" @onFinish="onFinishCategory" />
        </van-col>
        <van-col span="6">
          <dropDown :title="'排序方式'" :popup-options="districtTypeDict" :popup-title="'请选择排序方式'" @onFinish="onFinishDistrict" />
        </van-col>
        <van-col span="6" align="right">
          <search @onFinish="onFinishSearch" />
        </van-col>
      </van-row>
    </div>

    <van-list
      v-model="tableLoading"
      :finished="finished"
      :error.sync="error"
      error-text="请求失败，点击重新加载"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <ul>
        <li v-for="(item, index) in tableData" :key="index" class="model_content_other model_content gl_clear">
          <div class="left gl_fl">
            <span class="info_rank">{{ item.finalRank }}</span>
          </div>
          <div class="right">
            <div class="info_name gl_ellipsis">{{ item.name }}</div>
            <div class="gl_clear">
              <div class="info_gold gl_fl">
                <div>
                  <img class="img_gold" src="@/assets/images/league/mb_gold.png">
                  <span class="txt_gold">
                    {{ item.subRankList[0] ? item.subRankList[0].subRankCount : '' }}
                  </span>
                </div>
                <div>
                  <img class="img_gold" src="@/assets/images/league/mb_silver.png">
                  <span class="txt_gold">
                    {{ item.subRankList[1] ? item.subRankList[1].subRankCount : '' }}
                  </span>
                </div>
                <div>
                  <img class="img_gold" src="@/assets/images/league/mb_bronze.png">
                  <span class="txt_gold">
                    {{ item.subRankList[2] ? item.subRankList[2].subRankCount : '' }}
                  </span>
                </div>
              </div>
              <div class="gl_fl">
                <span class="info_score">总分 <span style="font-weight: 500">{{ item.finalPoints }}</span></span>
              </div>
            </div>
          </div>
        </li>
      </ul>
    </van-list>
  </div>
</template>

<script>
import dropDown from '../components/dropDown.vue';
import search from '../components/search.vue';
import {
  api_GET_EventsManagementEventResultUnifiedPointRankList,
  api_GET_EventsManagementCategory // 获取项目类别列表
} from '@/extend/api/gameswaggerApi/app';

import {
  Api_GET_EventsManagementEventResultUnifiedPointRankListQO
} from '@/lib/form/game/swaggerForm';

import defaultPhoto from '@/assets/images/face_default.png';

export default {
  components: {
    dropDown,
    search
  },
  props: {
    gameInfo: {
      type: Object,
      default () {
        return {};
      }
    }
  },
  data () {
    const _qo = new Api_GET_EventsManagementEventResultUnifiedPointRankListQO();
    _qo.DistrictType = 'Province';
    return {
      defaultPhoto: defaultPhoto, // 默认照片
      pageLoading: false,
      tableLoading: false,
      finished: false,
      error: false,
      qo: _qo,
      total: 0,
      tableData: [],
      categoryList: [],
      categoryFieldNames: {
        text: 'name',
        value: 'id',
        children: 'children'
      },
      topListType: '',
      topListdict: [
        {
          text: '总榜',
          value: null
        },
        {
          text: '竞技榜',
          value: 'Competitive'
        },
        {
          text: '品势榜',
          value: 'NoneCompetitive'
        }
      ],
      districtTypeDict: [
        {
          text: '省',
          value: 'Province'
        },
        {
          text: '市',
          value: 'City'
        },
        {
          text: '区县',
          value: 'County'
        }
      ]
    };
  },
  computed: {
    categoryFilterList () {
      if (this.topListType) {
        const _list = this.categoryList.filter(item => item.eventTypeCode === this.topListType);
        _list.unshift({
          name: '全部',
          id: null
        });
        return _list;
      } else {
        return this.categoryList;
      }
    }
  },
  watch: {
    gameInfo: {
      immediate: true,
      deep: true,
      handler(val) {
        if (val.id) {
          this.getPageList(1);
          this.getCategoryList({
            CompetitionId: this.gameInfo.id
          });
        }
      }
    }
  },
  methods: {
    onLoad() {
      let _page = this.qo.Page || 1;
      _page++;
      this.qo.Page = _page;
      this.getPageList(_page);
    },
    // 获取页面数据
    getPageList (page) {
      const _qo = JSON.parse(JSON.stringify(this.qo));
      _qo.Page = page || 1;
      _qo.SkipCount = (_qo.Page - 1) * _qo.MaxResultCount;
      _qo.CompetitionId = this.gameInfo.id;
      _qo.Type = 'District';
      _qo.EventTypeCode = this.topListType || null;
      _qo.IncludeFinal = true;
      _qo.IncludeSubRank = true;
      console.log('_qo: ', _qo);
      this.qo.Page = _qo.Page;
      this.apiRankList(_qo);
    },
    // 获取大项列表
    getCategoryList (qo) {
      api_GET_EventsManagementCategory(qo)
        .then(
          res => {
            console.log(res);
            if (res) {
              const _list = res.items || [];
              _list.unshift({
                name: '全部',
                id: null
              });
              this.categoryList = _list;
            }
          }
        );
    },
    // UnifiedPointRankList
    apiRankList(qo) {
      this.tableLoading = true;
      api_GET_EventsManagementEventResultUnifiedPointRankList(qo)
        .then(
          res => {
            this.tableLoading = false;
            this.total = res.totalCount;

            // 如果是第一页，表示需要覆盖整个内面内容，
            // 如果不是第一页，表示滑动加载的内容，需要push
            const _this = this;
            if (_this.qo.Page <= 1) {
              this.tableData = res.items;
            } else {
              this.tableData.push(...res.items);
            }

            // 数据全部加载完成
            if (this.tableData.length >= this.total) {
              this.finished = true;
            }
          }
        )
        .catch(
          error => {
            this.error = true;
            this.tableLoading = false;
            console.log(error);
          }
        );
    },
    getStaticFileUrl(relativeUrl) {
      if (!relativeUrl) return '';
      return WEB_CONFIG.VUE_APP_STATIC_FILE_RUL + relativeUrl;
    },
    errorPhoto() {
      const img = event.srcElement;
      img.src = this.defaultPhoto;
      img.onerror = null;
    },
    onFinishCategory (data) {
      console.log(data);
      const _category = (data && data[0]) || null;
      this.qo.CategoryId = _category.id;
      this.getPageList(1);
    },
    onFinishType (data) {
      const _type = (data && data[0]) || null;
      this.$refs.categoryDropDown.resetFieldValue();
      this.qo.CategoryId = null;
      this.topListType = _type.value;
      this.getPageList(1);
    },
    onFinishSearch (data) {
      this.qo.Keyword = data;
      this.getPageList(1);
    },
    onFinishDistrict (data) {
      const _type = (data && data[0]) || null;
      this.qo.DistrictType = _type.value;
      this.getPageList(1);
    }
  }
};
</script>

<style lang="scss" scoped>
  .control_filter_wrap {
    padding: 10px 20px;
  }
</style>

<style lang="scss" scoped>
/* 模块样式 */
  .main_content .model_content {
    margin-bottom: 5px;
  }
  .model_content_other {
    padding: 15px 7px 14px;
  }

/* 排名与Logo; 独立模块 */
  .info_rank {
    display: inline-block;
    width: 18px;
    height: 18px;
    line-height: 18px;
    font-size: 14px;
    color: #E02020;
    background-color: #ffe7e7;
    border-radius: 50%;
    text-align: center;
    margin-right: 9px;
  }
  .info_name {
    width: 100%;
    font-size: 15px;
    font-weight: 400;
    text-align: left;
    line-height: 21px;
    margin-bottom: 6px;
  }
  .info_gold {
    display: flex;
    width: 185px;
    justify-content: space-between;
    margin: 0 10px 10px 0;
  }
  .info_score {
    display: inline-block;
    padding: 1px 6px;
    font-size: 13px;
    font-weight: 400;
    text-align: left;
    color: #c28e00;
    line-height: 19px;
    background-color: #fff3d1;
    border-radius: 5px;
  }
  .img_gold {
    width: 13px;
    height: 19px;
    vertical-align: top;
    margin-right: 8px;
  }
  .txt_gold {
    display: inline-block;
    min-width: 26px;
    font-size: 14px;
    font-weight: 400;
    text-align: left;
    color: #666666;
    line-height: 20px;
    vertical-align: top;
  }

/* 模块差异 */
  .model_content_other .right {
    margin-left: 27px;
  }
  .model_content_other .info_rank {
    margin-top: 2px;
  }

</style>
